package com.raising.modules.sys.controller;

import com.raising.StaticParam;
import com.raising.framework.controller.BaseController;
import com.raising.framework.entity.ResultVo;
import com.raising.framework.interceptor.Page;
import com.raising.modules.sys.entity.ScheduleJob;
import com.raising.modules.sys.service.ScheduleJobService;
import com.raising.modules.sys.utils.DictUtils;
import com.raising.utils.JStringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 任务调度 控制器
 * @author gaoy
 * @version 1.0
 */
@Controller
@RequestMapping("${adminPath}/scheduleJob")
public class ScheduleJobController extends BaseController {

	@Autowired
	private ScheduleJobService scheduleJobService;


	/**
	 * 进入任务计划管理页面
	 * @author GaoYuan
	 * @datetime 2018/2/23 上午10:42
	 */
	@RequiresPermissions("sys:scheduleJob:view")
	@RequestMapping()
	public String list() {
		olog("进入任务计划管理页面");
		return "sys/scheduleJob/list";
	}

	/**
	 * 查询任务表列表数据【json】
	 * @author shimj
	 * @datetime 2017/12/16 下午14:10
	 */
	@RequiresPermissions("sys:scheduleJob:view")
	@RequestMapping(value = "/list.json",method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public ResultVo listJson(ScheduleJob scheduleJob,Page<ScheduleJob> page) throws Exception{
		page.setEntity(scheduleJob);
		return ResultVo.success().data(scheduleJobService.getPage(page));
	}

	/**
	 * 跳转到新增或编辑页
	 * @author shimj
	 * @datetime 2017/12/18 上午9:10
	 */
	@RequiresPermissions("sys:scheduleJob:update")
	@RequestMapping(value = "/update", method = RequestMethod.GET)
	public String update(String id,Model model) throws Exception{
		ScheduleJob scheduleJob;
		if(JStringUtils.isBlank(id)){
			scheduleJob = new ScheduleJob();
		}else{
			scheduleJob = scheduleJobService.get(id);
		}
		model.addAttribute("scheduleJob", scheduleJob);
		// 是否同步
		model.addAttribute("yn", DictUtils.getDictList("DIM_YN"));
		return "sys/scheduleJob/edit";
	}

	/**
	 * 保存任务
	 * @author shimj
	 * @datetime 2017/12/18 上午9:10
	 */
	@RequiresPermissions("sys:scheduleJob:update")
	@RequestMapping(value = "/update.json", method = RequestMethod.POST)
	@ResponseBody
	public ResultVo updateJson(ScheduleJob scheduleJob) throws Exception{
		scheduleJob.setStatus(StaticParam.VALID_STRING);
		if (scheduleJob.getIsNewId()) {
			olog("新增任务["+scheduleJob.getJobName()+"]");
		}else{
			olog("修改任务["+scheduleJob.getJobName()+"]","{id:"+scheduleJob.getId()+"}");
		}
		return scheduleJobService.save(scheduleJob);
	}

//	/**
//	 * 删除任务[不给删除]
//	 * @author shimj
//	 * @datetime 2017/12/18 上午9:20
//	 */
//	@RequiresPermissions("sys:scheduleJob:delete")
//	@RequestMapping(value = "/delete", method = RequestMethod.GET)
//	public String delete(ScheduleJob scheduleJob,int pageNo,int pageSize,
//			RedirectAttributes redirectAttributes) {
//		scheduleJobService.delete(scheduleJob);
//		try {
//			scheduleJobService.deleteJob(scheduleJob);
//		} catch (SchedulerException e) {
//			e.printStackTrace();
//		}
//		addMessage(redirectAttributes, "删除成功");
//		return "redirect:" + adminPath + "/scheduleJob?pageNo="+pageNo+"&pageSize="+pageSize;
//	}

	/**
	 * 更改任务状态
	 * @author gaoyuan
	 * @param scheduleJob
	 * @return
	 */
	@RequiresPermissions("sys:scheduleJob:changeJobStatus")
	@RequestMapping(value = "/changeJobStatus.json", method = RequestMethod.POST)
	@ResponseBody
	public ResultVo changeJobStatusJson(ScheduleJob scheduleJob) throws Exception{
		String status = scheduleJob.getStatus();
		String id = scheduleJob.getId();
		scheduleJob = scheduleJobService.get(id);
		scheduleJob.setStatus(status);
		scheduleJobService.changeJobStatus(scheduleJob);
		olog("更改任务状态","{id:"+scheduleJob.getId()+"}");
		return ResultVo.success().desc("任务状态更改成功");
	}

	/**
	 * 暂停任务
	 * @author gaoyuan
	 * @param scheduleJob
	 * @return
	 */
	@RequiresPermissions("sys:scheduleJob:changeJobStatus")
	@RequestMapping(value = "/pauseJob.json", method = RequestMethod.POST)
	@ResponseBody
	public ResultVo pauseJobJson(ScheduleJob scheduleJob) throws Exception {
		try {
			String status = scheduleJob.getStatus();
			String id = scheduleJob.getId();
			scheduleJob = scheduleJobService.get(id);
			scheduleJob.setStatus(status);
			scheduleJobService.pauseJob(scheduleJob);
			olog("暂停任务["+scheduleJob.getJobName()+"]","{id:"+scheduleJob.getId()+"}");
			return ResultVo.success().desc("任务已暂停").data(scheduleJob.getId());
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
		return ResultVo.error().desc("任务暂停失败").data(scheduleJob.getId());
	}

	/**
	 * 恢复任务
	 * @author gaoyuan
	 * @return
	 */
	@RequiresPermissions("sys:scheduleJob:changeJobStatus")
	@RequestMapping(value = "/resumeJob.json", method = RequestMethod.POST)
	@ResponseBody
	public ResultVo resumeJobJson(ScheduleJob scheduleJob) throws Exception{
		try {
			String status = scheduleJob.getStatus();
			String id = scheduleJob.getId();
			scheduleJob = scheduleJobService.get(id);
			scheduleJob.setStatus(status);
			scheduleJobService.resumeJob(scheduleJob);
			olog("恢复任务["+scheduleJob.getJobName()+"]","{id:"+scheduleJob.getId()+"}");
			return ResultVo.success().desc("任务已恢复").data(scheduleJob.getId());
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
		return ResultVo.error().desc("任务恢复失败").data(scheduleJob.getId());
	}

	/**
	 * 立即执行任务
	 * @author gaoyuan
	 * @return
	 */
	@RequiresPermissions("sys:scheduleJob:changeJobStatus")
	@RequestMapping(value = "/runAJobNow.json", method = RequestMethod.POST)
	@ResponseBody
	public ResultVo runAJobNowJson(ScheduleJob scheduleJob) throws Exception{
		try {
			scheduleJob = scheduleJobService.get(scheduleJob.getId());
			scheduleJob.setMethodName("run");
			scheduleJobService.runAJobNow(scheduleJob);
			olog("手动执行任务["+scheduleJob.getJobName()+"]","{id:"+scheduleJob.getId()+"}");
			return ResultVo.success().desc("成功执行任务");
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
		return ResultVo.error().desc("执行任务失败");
	}


}
